#include "mg_LP5036.h"

//毫秒级延时（非阻塞）
void RGB_Delay_Ms(uint32_t ms)
{
	usleep(1000 * ms);
}
//RGB芯片GPIO初始化
void RGB_GPIO_Init(void)
{
	ithGpioSetMode(LP5036_EN_Pin, ITH_GPIO_MODE0);
	ithGpioSetOut(LP5036_EN_Pin);
}
//打开LP5036芯片使能
void RGB_LED_Enable(void)
{
	ithGpioSet(LP5036_EN_Pin);
}
//打开LP5036芯片使能
void RGB_LED_Disable(void)
{
	ithGpioClear(LP5036_EN_Pin);
}
//写寄存器
void LP5036WriteReg(uint8_t REG_Address, uint8_t REG_Data)
{
	I2C0_Write_8Reg(RGB_IIC_ADDR, REG_Address, &REG_Data, 1);
}
//写多个寄存器
void LP5036WriteMultiReg(uint8_t Start_REG_Address, uint8_t *dat, uint8_t data_len)
{
	I2C0_Write_8Reg(RGB_IIC_ADDR, Start_REG_Address, dat, data_len);
}
//设置LED控制模式为总控
void Led_Set_General_Control(void)
{
	uint8_t Config_Data[2] = { 0xFF, 0x0F };
	LP5036WriteMultiReg(Led_Config0, Config_Data, 2);
}
//设置LED控制模式为单控
void Led_Set_Single_Control(void)
{
	uint8_t Config_Data[2] = { 0, 0 };
	LP5036WriteMultiReg(Led_Config0, Config_Data, 2);
}
//设置单颗Led亮度
void Led_Brightness_Set(uint8_t RGB_ID, uint8_t Brightness)
{
	uint8_t PCB_RGB_ID;
	PCB_RGB_ID = 11 - RGB_ID;
	LP5036WriteReg(PCB_RGB_ID + 8, Brightness);
}
//设置所有Led亮度
void Led_AllBrightness_Set(uint8_t *Brightness)
{
	Led_Set_Single_Control();//设为单控
	LP5036WriteMultiReg(Led0_Brigtness, Brightness, RGB_NUM);
}
//设置单颗Led颜色
void Led_Color_Set(uint8_t RGB_ID, uint8_t R, uint8_t G, uint8_t B)
{
	uint8_t PCB_RGB_ID;
	uint8_t RGB_Data[3] = { R, G, B };
	PCB_RGB_ID = 11 - RGB_ID;
	LP5036WriteMultiReg(0x14 + PCB_RGB_ID * 3, RGB_Data, sizeof(RGB_Data));
}
//RGB总控
void All_Led_Set(uint8_t Brightness, uint8_t R, uint8_t G, uint8_t B)
{
	uint8_t Data[6] = { 0xFF, 0x0F, Brightness, R, G, B };
	LP5036WriteMultiReg(Led_Config0, Data, sizeof(Data));//总控
}
//RGB单控(RGB_ID:0-11)颜色参数:0x11,0x22,0x33
void Single_Led_Set(uint8_t RGB_ID, uint8_t Brightness, uint8_t R, uint8_t G, uint8_t B)
{
	uint8_t PCB_RGB_ID;
	uint8_t RGB_Data[3] = { R, G, B };
	PCB_RGB_ID = 11 - RGB_ID;

	Led_Set_Single_Control();//切换为单控
	LP5036WriteReg(PCB_RGB_ID + 8, Brightness);
	LP5036WriteMultiReg(0x14 + PCB_RGB_ID * 3, RGB_Data, sizeof(RGB_Data));
}
//RGB单控(RGB_ID:0-11)颜色参数:0x112233
void Single_Led_Set_RGB(uint8_t RGB_ID, uint8_t Brightness, uint32_t RGB_Data)
{
	uint8_t PCB_RGB_ID;
	uint8_t RGB_Data_Temp[3];
	RGB_Data_Temp[0] = (RGB_Data & 0xFF0000) >> 16;
	RGB_Data_Temp[1] = (RGB_Data & 0x00FF00) >> 8;
	RGB_Data_Temp[2] = (RGB_Data & 0x0000FF) >> 0;
	PCB_RGB_ID = 11 - RGB_ID;

	Led_Set_Single_Control();//切换为单控
	LP5036WriteReg(PCB_RGB_ID + 8, Brightness);
	LP5036WriteMultiReg(0x14 + PCB_RGB_ID * 3, RGB_Data_Temp, sizeof(RGB_Data_Temp));
}
//互斥RGB单控(RGB_ID:0-11)颜色参数:0x11,0x22,0x33
void Only_Led_Set(uint8_t RGB_ID, uint8_t Brightness, uint8_t R, uint8_t G, uint8_t B)
{
	uint8_t i;
	uint8_t PCB_RGB_ID;
	PCB_RGB_ID = 11 - RGB_ID;
	All_Led_Set(0, 0, 0, 0);//全关
	Single_Led_Set(PCB_RGB_ID, Brightness, R, G, B);
}
//互斥RGB单控(RGB_ID:0-11)颜色参数:0x112233
void Only_Led_Set_RGB(uint8_t RGB_ID, uint8_t Brightness, uint32_t RGB_Data)
{
	uint8_t i;
	uint8_t PCB_RGB_ID;
	PCB_RGB_ID = 11 - RGB_ID;
	All_Led_Set(0, 0, 0, 0);//全关
	Single_Led_Set_RGB(PCB_RGB_ID, Brightness, RGB_Data);
}
//按照百分比显示环状LED
#define LED_PWM_GRADE	12
static uint8_t Led_Brightness[RGB_NUM] = { 0 };
void LED_Progress_Bar(uint16_t Angle)
{
	uint8_t Led_On_ID;
	uint32_t RGB_Data = 0x0000FF;
	if (Angle == 360)Angle = 0;
	Led_On_ID = Angle / 30;
	Led_Brightness[11-Led_On_ID] = 0xFF;
	RGB_LED_Enable();
	Single_Led_Set_RGB(Led_On_ID, 0xFF, RGB_Data);

	////12颗LED总的进度是1200
	//uint16_t Num_Pwm = Percent*LED_PWM_GRADE;
	//uint8_t Led_On_Num, Last_Led_On_Pwm, Led_Off_Num, Bar_Led_ID;
	//uint8_t i;
	//uint8_t RGB_Data[3] = { 0, 0xff, 0xff };
	//Led_On_Num = Num_Pwm / 100;
	//Last_Led_On_Pwm = Num_Pwm % 100;
	//Led_Off_Num = 12 - Led_On_Num-1;
	//
	//Led_Set_Single_Control();
	////全亮的LED
	//for (i = 0; i < Led_On_Num; i++)
	//{
	//	Bar_Led_ID = 11 - i;
	//	LP5036WriteReg(Bar_Led_ID + 8, 0xFF);
	//	LP5036WriteMultiReg(0x14 + Bar_Led_ID * 3, RGB_Data, sizeof(RGB_Data));
	//}
	////最后一颗亮Led的亮度输出
	//Bar_Led_ID = 11 - Led_On_Num;
	//LP5036WriteReg(Bar_Led_ID + 8, Last_Led_On_Pwm*0xFF/100);
	//LP5036WriteMultiReg(0x14 + Bar_Led_ID * 3, RGB_Data, sizeof(RGB_Data));
	////全灭的LED
	//for (i = Led_On_Num + 1; i < 12; i++)
	//{
	//	Bar_Led_ID = 11 - i;
	//	LP5036WriteReg(Bar_Led_ID + 8, 0);
	//	LP5036WriteMultiReg(0x14 + Bar_Led_ID * 3, RGB_Data, sizeof(RGB_Data));
	//}
}
//逐渐环状LED
#define Die_Grade		0x02
bool DieOutEn = false;
void LedCircleDieOut(void)
{
	uint8_t i, Change_Flag = 0;
	if (!DieOutEn)return;
	DieOutEn = false;
	//更新LedPwm
	for (i = 0; i<RGB_NUM; i++)
	{
		if (Led_Brightness[11 - i] != 0)
		{
			Change_Flag = 1;
			if (Led_Brightness[11 - i] >= Die_Grade)
				Led_Brightness[11 - i] -= Die_Grade;
			else
				Led_Brightness[11 - i] = 0;
		}
	}
	if (Change_Flag)//如果PWM有变化
	{
		RGB_LED_Enable();
		Led_AllBrightness_Set(Led_Brightness);
	}
	else
	{
		RGB_LED_Disable();
	}
}
//RGB测试
void RGB_TEST(void)
{
	int i;
	uint8_t RGBData[3] = { 0x00, 0xFF, 0xFF };
	//White灯
	for (i = 0; i <= 0xFF; i++)
	{
		All_Led_Set(i, 0, i, i);//W
	}
	//White灯
	for (i = 0xFF; i >= 0; i--)
	{
		All_Led_Set(i, 0, i, i);//W
	}
	//White灯
	for (i = 0; i <= 0xFF; i++)
	{
		Single_Led_Set_RGB(0, i, RGBData);//W
	}
	//White灯
	for (i = 0xFF; i >= 0; i--)
	{
		Single_Led_Set_RGB(0, i, RGBData);//W
	}

}
//RGB驱动初始化
void RGB_LED_Init(void)
{
	RGB_LED_Enable();//打开使能
	//RGB_LED_Disable();//关闭使能
	LP5036WriteReg(Device_Config0, 0x40);
}
